package com.base.cn.platform.os.common.mybatis;

import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 获取对象所有的属性的工具类
 */
public class MyBatisFieldUtil<E> {

    /**
     * 获取对象的属性
     * @param entityClazz
     * @param entity
     * @param <E>
     * @return
     */
    public static <E> Field[] getEntityFields(Class entityClazz ,E entity){
        if(!ObjectUtils.isEmpty(entityClazz)){
            List<Field> fieldList = new ArrayList<Field>();

            getAllField(entityClazz,fieldList);
            if(!fieldList.isEmpty()){
                List<Field> _fieldList = new ArrayList<>();
                int i=0;
                for(Field field : fieldList){
                    try {
                        field.setAccessible(true);
                        Object value = field.get(entity);
                        if(value!=null){
                            _fieldList.add(field);
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                Field[] fields = _fieldList.toArray(new Field[_fieldList.size()]);
                return fields;
            }
        }
        return null;
    }
    private static void getAllField(Class clazz,List<Field> fieldList){
        Field[] fields = clazz.getDeclaredFields();
        if(!ObjectUtils.isEmpty(fields)){
            for(Field field : fields){
                if(checkField(field)){
                    fieldList.add(field);
                }
            }
        }
        Class superClass = clazz.getSuperclass();
        if(!ObjectUtils.isEmpty(superClass) && IEntity.class.isAssignableFrom(superClass)){
            getAllField(superClass,fieldList);
        }
    }

    private static boolean checkField(Field field){
        SqlExclude sqlExclude = field.getAnnotation(SqlExclude.class);
        if(sqlExclude !=null){
            return false;
        }
        //排序静态属性和final属性
        if(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())){
            return false;
        }
        if(field.getType()==String.class){
            return true;
        }
        if(field.getType()==Integer.class){
            return true;
        }
        if(field.getType()==Long.class){
            return true;
        }
        if(field.getType()==Double.class){
            return true;
        }
        if(field.getType()==Boolean.class){
            return true;
        }
        if(field.getType()==Short.class){
            return true;
        }
        if(field.getType()==Float.class){
            return true;
        }
        if(field.getType()==Date.class){
            return true;
        }
        if(field.getType()==Timestamp.class){
            return true;
        }
        if(field.getType()==BigDecimal.class){
            return true;
        }
        return false;
    }

}
